package Project2.cs342.project1.mathNotebook.data;

import java.io.*;

/**
 * Creates XML based NotebookReader and NotebookWriter objects
 * @author Jared Schlicht
 *
 */
public class XMLNotebookRepositoryFactory extends NotebookRepositoryAbstractFactory {
	private String filename;
	
	public XMLNotebookRepositoryFactory(String filename) {
		this.filename = filename;
	}

	/**
	 * Creates a new XMLNotebookReader.
	 * @return NotebookReader or null if an error occurred
	 * @author Jared Schlicht
	 */
	@Override
	public NotebookReader createReader() {
	    NotebookReader reader = null;
	    
	    // ensure the file exists first
	    File file = new File(this.filename);
	    try {
		file.createNewFile();
	    } catch (IOException e1) {
	    }
	    
	    try {
		reader = new XMLNotebookReader(new FileInputStream(file));
	    } catch (FileNotFoundException e) {
		reader = null;
	    }
	    
	    return reader;
	}

	/**
	 * Creates a new XMLNotebookWriter
	 * @return NotebookWriter or null if an error occurred
	 * @author Jared Schlicht
	 */
	@Override
	public NotebookWriter createWriter() {
	    NotebookWriter writer = null;
	    
	    // ensure the file exists first
	    File file = new File(this.filename);
	    try {
		file.createNewFile();
	    } catch (IOException e1) {
	    }
	    
	    try {
		writer = new XMLNotebookWriter(new FileOutputStream(file));
	    } catch (FileNotFoundException e) {
		writer = null;
	    }
	    
	    return writer;
	}

}
